﻿using ProjectCommonLibs.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCommonLibs.repository
{
    public class DataAggregateUtils
    {
        /// <summary>
        /// 因为数据点太多，所以需要把数据点都聚合一下
        /// 效果一致，但是会大幅减轻前端浏览的时候的压力
        /// </summary>
        /// <param name="rawData"></param>
        /// <returns></returns>
        public static List<SeasonalEfficiencyModel> aggregateSeasonalData(List<SeasonalEfficiencyModel> rawData)
        {
            int factor = 1;
            if (rawData.Count == 0)
            {
                return rawData;
            }
            //if((rawData.Max(s => s.power) - rawData.Min(s => s.power)) < 50)
            //{
            //    factor = 5;
            //}
            rawData.ForEach(r => r.power *= factor);
            var classified = rawData.GroupBy(r => (int)r.power).ToList();
            
            rawData.Clear();
            foreach(var item in classified)
            {
                var key = item.Key;
                var count = 0;
                var sum = 0.0;
                foreach(var seasonData in item)
                {
                    count++;
                    sum += seasonData.efficiency;
                }
                var avg = 0.0;
                if (count != 0)
                {
                    avg = sum / count;
                }
                rawData.Add(new SeasonalEfficiencyModel() { power = key * 1.0 / factor, efficiency = avg });
            }
            return rawData;
        }
    }
}
